package com.example.text.cyclone;



import android.text.format.DateUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DateUtil {
    public static final String DEFAULT_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm";
    public static final String DEFAULT_DATE_FORMAT_S = "yyyy/MM/dd HH:mm";
    public static final String SHORT_DATE_FORMAT = "MM-dd HH:mm";
    public static final String SHORT_DATE_FORMAT_H_ONLY = "H";
    public static final String SHORT_DATE_FORMAT_MMDD = "MM-dd";
    public static final String SHORT_TIME_FORMAT = "HH:mm";
    public static final String SHORT_SECOND_FORMAT = "HH:mm:ss";
    public static final String MONTH_DATE_FORMAT = "yyyy-MM";
    public static final String MONTH2_DATE_FORMAT = "yyyy年MM月";
    public static final String POINT_DATE_FORMAT = "yyyy.MM.dd";
    public static final String CHINESE_DATE_FORMAT = "yyyy年MM月dd日";
    public static final String DATE_FORMAT_Y_M = "yyyy-MM";
    public static final String DATE_FORMAT_Y_M_D = "yyyy-MM-dd";
    public static final String DATE_FORMAT_Y_M_D_H_M = "yyyy-MM-dd HH:mm";
    public static final String DATE_FORMAT_Y_M_D_H_M_S = "yyyy-MM-dd HH:mm:ss";


    /*
    凌晨
    00:00:00----04:59:59
    早上
    05:00:00----07:59:59
    上午
    08:00:00----10:59:59
    中午
    11:00:00----12:59:59
    下午
    13:00:00----16:59:59
    傍晚
    17:00:00----18:59:59
    晚上
    19:00:00----23:59:59
    */
    public static String transHour(long time) {
        int hour = Integer.parseInt(formatDate(time, SHORT_DATE_FORMAT_H_ONLY));
        if (hour < 5)
            return "凌晨";
        if (hour < 8)
            return "早上";
        if (hour < 11)
            return "上午";
        if (hour < 13)
            return "中午";
        if (hour < 17)
            return "下午";
        if (hour < 19)
            return "傍晚";
        if (hour < 24)
            return "晚上";
        return "";
    }

    public static String formatDate(Date date, String format) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format, Locale.CHINA);
        return dateFormat.format(date);
    }

    public static String formatDate(Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(DEFAULT_DATE_FORMAT, Locale.CHINA);
        return dateFormat.format(date);
    }

    public static String formatDate(long millis, String format) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format, Locale.CHINA);
        return dateFormat.format(new Date(millis));
    }

    public static String formatDate(long millis) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(DEFAULT_FORMAT, Locale.CHINA);
        return dateFormat.format(new Date(millis));
    }

    public static String formatDateWithWeek(long millis) {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat dateFormat = new SimpleDateFormat(DEFAULT_DATE_FORMAT, Locale.CHINA);
        calendar.setTime(new Date(millis));
        int weekIndex = calendar.get(Calendar.DAY_OF_WEEK);
        String week = "";
        switch (weekIndex) {
            case 1:
                week = "星期日";
                break;
            case 2:
                week = "星期一";
                break;
            case 3:
                week = "星期二";
                break;
            case 4:
                week = "星期三";
                break;
            case 5:
                week = "星期四";
                break;
            case 6:
                week = "星期五";
                break;
            case 7:
                week = "星期六";
                break;
        }
        String date = dateFormat.format(new Date(millis));
        StringBuilder dateBuilder = new StringBuilder(date);
        dateBuilder.insert(10, " " + week);
        return dateBuilder.toString();
    }

    /**
     * 获取当天时间以及星期：ep:2019-08-12 星期二
     */
    public static String formatDateWithWeekCurrent() {
        long millis = System.currentTimeMillis();
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT_Y_M_D, Locale.CHINA);
        calendar.setTime(new Date(millis));
        int weekIndex = calendar.get(Calendar.DAY_OF_WEEK);
        String week = "";
        switch (weekIndex) {
            case 1:
                week = "星期日";
                break;
            case 2:
                week = "星期一";
                break;
            case 3:
                week = "星期二";
                break;
            case 4:
                week = "星期三";
                break;
            case 5:
                week = "星期四";
                break;
            case 6:
                week = "星期五";
                break;
            case 7:
                week = "星期六";
                break;
        }
        String date = dateFormat.format(new Date(millis));
        return date + " " + week;
    }

    /**
     * 从 HH:mm:ss 转成 HH:mm
     */
    public static String formatTo(String time, String formatFrom, String formatTo) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(formatTo, Locale.CHINA);
        return dateFormat.format(dateToTimeStamp(time, formatFrom));
    }

    public static Calendar returnCalendar(String time, String format) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(dateToTimeStamp(time, format)));
        return calendar;
    }

    public static String shortTimeformatDate(long millis) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(SHORT_TIME_FORMAT, Locale.CHINA);
        return dateFormat.format(new Date(millis));
    }

    /**
     * 时间转化为 yyyy.MM.dd
     *
     * @param millis 时间ms
     * @return yyyy.MM.dd
     */
    public static String pointDateFormat(long millis) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(POINT_DATE_FORMAT, Locale.CHINA);
        return dateFormat.format(new Date(millis));
    }

    /**
     * 时间转化为 yyyy-MM-dd
     *
     * @param millis 时间ms
     * @return yyyy-MM-dd
     */
    public static String dividerDateFormat(long millis) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT_Y_M_D, Locale.CHINA);
        return dateFormat.format(new Date(millis));
    }

    public static Boolean isOver2Year(long millis) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(millis);
        calendar.add(Calendar.YEAR, 2);
        return 1 == compareDate(new Date(System.currentTimeMillis()), calendar.getTime());
    }

    public static Boolean isWaitOver2Year(long millis) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(millis);
        calendar.add(Calendar.YEAR, 2);
        return (-1 == compareDate(new Date(System.currentTimeMillis()), calendar.getTime()))
                && (calendar.getTimeInMillis() - System.currentTimeMillis() <= 7 * 24 * 3600 * 1000);
    }

    // >:1  <:-1  =:0
    public static int compareDate(Date d1, Date d2) {
        return Long.compare(d1.getTime(), d2.getTime());
    }

    /**
     * 时间转化为 yyyy年MM月dd日
     *
     * @param millis 时间ms
     */
    public static String chineseDateFormat(long millis) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(CHINESE_DATE_FORMAT, Locale.CHINA);
        return dateFormat.format(new Date(millis));
    }

    public static Integer getMonth(long millis) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(millis);
        return calendar.get(Calendar.MONTH);
    }

    /**
     * 计算过期时间，返回：天
     */
    public static int calcExpirationDays(String date) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_Y_M_D, Locale.CHINA);
            long time = sdf.parse(date).getTime();
            long currentTimeMillis = System.currentTimeMillis();
            // 加一天
            time += DateUtils.DAY_IN_MILLIS;
            if (time <= currentTimeMillis)// 已过期
                return 0;
            float gap = time - currentTimeMillis;
            return (int) Math.ceil(gap / DateUtils.DAY_IN_MILLIS);
        } catch (ParseException e) {
            e.printStackTrace();
            return 0;
        }
    }

    public static long dateToTimeStamp(String date, String format) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.CHINA);
            return sdf.parse(date).getTime();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return System.currentTimeMillis();
    }

    public static String countTimer(Long time) {
        int countTime = time.intValue();
        final int MINUTE = 60;
        final int HOUR = 60 * 60;
        String hour = "";
        if ((int) countTime / HOUR < 10) {
            hour = "0" + (int) countTime / HOUR;
        } else {
            hour = "" + (int) countTime / HOUR;
        }

        String min = "";
        if ((int) countTime % HOUR / MINUTE < 10) {
            min = "0" + (int) countTime % HOUR / MINUTE;
        } else {
            min = "" + (int) countTime % HOUR / MINUTE;
        }

        String second = "";
        if (countTime % MINUTE < 10) {
            second = "0" + countTime % MINUTE;
        } else {
            second = "" + countTime % MINUTE;
        }
        return hour + ":" + min + ":" + second;
    }

    //时间转成上午，下午格式
    public static String transformDate(long time) {
        SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("a hh:mm", Locale.CHINA);
        Date date2 = new Date(time);
        return simpleDateFormat2.format(date2);
    }

    public static String showTime(String time, String format) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format, Locale.CHINA);
        return transformDate(simpleDateFormat.parse(time).getTime());
    }

    public static String getCarTime(String time_from, String time_to) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(SHORT_SECOND_FORMAT, Locale.CHINA);
        long from = simpleDateFormat.parse(time_from).getTime();
        long to = simpleDateFormat.parse(time_to).getTime();
        if (from > to) {
            return transformDate(from) + "至次日" + transformDate(to);
        } else {
            return transformDate(from) + "至" + transformDate(to);
        }
    }

    public static String getToday() {
        return formatDate(System.currentTimeMillis(), DATE_FORMAT_Y_M_D);
    }

    public static String currMonth() {
        return formatDate(System.currentTimeMillis(), MONTH_DATE_FORMAT);
    }
}

